{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 241022-《零基础学量化》免费系列公开课Class 1 \n",
    "WorldQuant-BRAIN-Beginner_Learning_Camp \\\n",
    "Chapter 1: Overview of Quant and its Key Concepts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from os import environ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 登录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201\n",
      "{'user': {'id': 'SZ75632'}, 'token': {'expiry': 14400.0}, 'permissions': ['REFERRAL']}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "from os.path import expanduser\n",
    "from requests.auth import HTTPBasicAuth\n",
    "\n",
    "# Load credentials\n",
    "with open(expanduser('D:\\\\code\\\\量化\\\\brain_use_pwd.txt')) as f:\n",
    "    credentials = json.load(f)\n",
    "\n",
    "# Extract username and password from the list\n",
    "username, password = credentials\n",
    "\n",
    "# Create a session object\n",
    "sess = requests.Session()\n",
    "\n",
    "# Set up basic authentication\n",
    "sess.auth = HTTPBasicAuth(username, password)\n",
    "\n",
    "# Send a POST request to the API for authentication\n",
    "response = sess.post('https://api.worldquantbrain.com/authentication')\n",
    "\n",
    "# Print response status and content for debugging\n",
    "print(response.status_code)\n",
    "print(response.json())\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 回测 Alpha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "simulation_data = {\n",
    "    'type' : 'REGULAR',\n",
    "    'settings' : {\n",
    "        'instrumentType' : 'EQUITY',\n",
    "        'region' : 'USA',\n",
    "        'universe' : 'TOP3000',\n",
    "        'delay' : 1,\n",
    "        'decay' : 0,\n",
    "        'neutralization' : 'INDUSTRY',\n",
    "        'truncation' : 0.08,\n",
    "        'pasteurization': 'ON',\n",
    "        'unitHandling' : 'VERIFY',\n",
    "        'nanHandling': 'OFF',\n",
    "        'language' :'FASTEXPR',\n",
    "        'visualization':False,\n",
    "    },\n",
    "    'regular' : 'liabilities/assets'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import sleep\n",
    "sim_resp = sess.post(\n",
    "    'https://api.worldquantbrain.com/simulations', \n",
    "    json = simulation_data,\n",
    ")\n",
    "print(sim_resp.headers)\n",
    "sim_progress_url = sim_resp.headers['Location']\n",
    "while True:\n",
    "    sim_progress_resp = sess.get(sim_progress_url)\n",
    "    retry_after_sec   = float(sim_progress_resp.headers.get(\"Retry-After\",0))\n",
    "    if retry_after_sec == 0:                  # simulation done!\n",
    "        break\n",
    "    sleep(retry_after_sec)\n",
    "alpha_id = sim_progress_resp.json()['alpha']  # the final simulation result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5lw9mvk'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpha_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': '3xhYBW3zQ4sG9Qgbz90IJQ5',\n",
       " 'type': 'REGULAR',\n",
       " 'settings': {'instrumentType': 'EQUITY',\n",
       "  'region': 'USA',\n",
       "  'universe': 'TOP3000',\n",
       "  'delay': 1,\n",
       "  'decay': 0,\n",
       "  'neutralization': 'INDUSTRY',\n",
       "  'truncation': 0.08,\n",
       "  'pasteurization': 'ON',\n",
       "  'unitHandling': 'VERIFY',\n",
       "  'nanHandling': 'OFF',\n",
       "  'language': 'FASTEXPR',\n",
       "  'visualization': False},\n",
       " 'regular': 'liabilities/assets',\n",
       " 'status': 'COMPLETE',\n",
       " 'alpha': '5lw9mvk'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sim_progress_resp.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "undefined.undefined.undefined"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
